package com.seu.data.structures.binarySearchTree.d.preorder;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
// Classic Non-Recursive algorithm for preorder traversal
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree

/**
 * 非递归实现二叉树的前序遍历
 *
 * @author liangfeihu
 * @since 2018/12/4 10:35
 */
public class Solution1 {

    public List<Integer> preorderTraversal(TreeNode root) {
        ArrayList<Integer> res = new ArrayList<Integer>();
        if (root == null) {
            return res;
        }

        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);
        while (!stack.empty()) {
            TreeNode curNode = stack.pop();
            res.add(curNode.val);

            if (curNode.right != null) {
                stack.push(curNode.right);
            }
            if (curNode.left != null) {
                stack.push(curNode.left);
            }
        }
        return res;
    }

}
